﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using CoolCode.Controls;

namespace CoolCode.Controls.UI
{
    public partial class ControlPanel : UserControl
    {
        public ControlPanel()
        {
            InitializeComponent();
            this.loadingGrid.Children.Add(new Loading { Visibility = Visibility.Collapsed });
            Layout();
        }

        public string Title
        {
            get { return HeaderTitle.Text; }
            set { value = HeaderTitle.Text; }
        }

        public virtual void Layout()
        {
            //HeaderTitle.Text = "Personal Financial Manager";
            //TreeView trvMenu = new TreeView();
            //trvMenu.Margin = new Thickness(2, 2, 2, 2);
            //TreeViewItem _menu1 = new TreeViewItem { Header = "Transaction", Foreground = new SolidColorBrush(Colors.Blue), IsExpanded = true };
            //_menu1.Items.Add(new TreeViewItem { Header = "Dayly Transaction", Tag = "TranList" });
            //_menu1.Items.Add(new TreeViewItem { Header = "Weekly Transaction" });
            //_menu1.Items.Add(new TreeViewItem { Header = "Monthly Transaction" });
            //_menu1.Items.Add(new TreeViewItem { Header = "Yearly Transaction" });
            //TreeViewItem _menu2 = new TreeViewItem { Header = "Master", Foreground = new SolidColorBrush(Colors.Blue), IsExpanded = true };
            //_menu2.Items.Add(new TreeViewItem { Header = "User Login", Tag = "Login" });
            //_menu2.Items.Add(new TreeViewItem { Header = "User Group Master", Tag = "UserGroup" });
            //_menu2.Items.Add(new TreeViewItem { Header = "User Regedit", Tag = "Regedit" });
            //_menu2.Items.Add(new TreeViewItem { Header = "User Info", Tag = "UserInfo" });
            //trvMenu.Items.Add(_menu1);
            //trvMenu.Items.Add(_menu2);
            //foreach (TreeViewItem rootItem in trvMenu.Items)
            //{
            //    foreach (TreeViewItem item in rootItem.Items)
            //    {
            //        if (item.Tag != null)
            //        {
            //            item.MouseLeftButtonUp += new MouseButtonEventHandler(item_MouseLeftButtonDown);
            //        }
            //    }
            //}

            //MenuPanel.Children.Add(trvMenu);

            //ShowPanel("Login", "Login");
            //TabControl tabMain = new TabControl { Name = "tabMain" };
            //tabMain.Items.Add(new TabItem { Header = "Main Page", Content = new Login() });
            //rightContent.Children.Add(tabMain);
            //this.dialogGrid.Children.Add(new Login { Name = "login" });
        }

        private void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem element = sender as TreeViewItem;
            if (element.Tag != null)
            {
                string panelName = element.Tag.ToString();
                string title = element.Header.ToString();
                Window.ShowLoading(title);
                ShowPanel(panelName, title);
                Window.HideLoading();
            }
        }

        protected void AddMenuPanel(UIElement element)
        {
            MenuPanel.Children.Add(element);
        }

        protected void ShowPanel(string panelName, string title)
        {
            if (panelName == "")
            {
                Window.Alert("This function hasn't implemented");
                return;
            }

            UserControl panel = this.FindName(string.Format("RightContent_{0}", panelName)) as UserControl;
            if (panel == null)
            {
                panel = this.CreatePanel(panelName);
                panel.Name = string.Format("RightContent_{0}", panelName);
                this.rightContent.Children.Add(panel);
            }
            else
            {
                this.rightContent.Children.Remove(panel);
                this.rightContent.Children.Add(panel);
            }
        }

        //private void ShowPanel(string panelName, string title)
        //{
        //    if (panelName == "")
        //    {
        //        Window.Alert("This function hasn't implemented");
        //        return;
        //    }
        //    TabItem tab = this.FindName(string.Format("tabpage_{0}", panelName)) as TabItem;
        //    if (tab == null)
        //    {
        //        UserControl panel = this.CreatePanel(panelName);
        //        if (panel == null)
        //        {
        //            Window.Alert("This function hasn't implemented");
        //            return;
        //        }
        //        panel.Name = string.Format("RightContent_{0}", panelName);
        //        panel.Margin = new Thickness(2, 2, 2, 2);
        //        TabControl tabMain = this.FindName("tabMain") as TabControl;
        //        if (tabMain == null)
        //            throw new Exception("No this Tab!");
        //        Border border = new Border
        //        {
        //            CornerRadius = new CornerRadius(10),
        //            BorderThickness = new Thickness(2, 2, 2, 2),
        //            Margin = new Thickness(2, 2, 2, 2)
        //        };
        //        border.Child = panel;
        //        tab = new TabItem { Name = string.Format("tabpage_{0}", panelName), Header = title, Content = border };
        //        tabMain.Items.Add(tab);
        //        tab.IsSelected = true;
        //    }
        //    else
        //    {
        //        tab.IsSelected = true;
        //    }
        //}

        private UserControl CreatePanel(string panelName)
        {
            string ns = this.GetType().Namespace;
            if (panelName != string.Empty)
            {
                System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
                foreach (Type type in executingAssembly.GetTypes())
                {
                    if (type.FullName.Equals(ns + "." + panelName) || type.FullName.Equals(panelName))
                    {
                        UserControl panel = Activator.CreateInstance(type) as UserControl;
                        return panel;
                    }
                }
            }
            return null;
        }
    }
}
